<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="资产编号" clearable></el-input>
      </el-form-item>

      <template>
        <el-select v-model="dataForm.key2" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.key2"
            :label="item.label"
            :value="item.key2">
          </el-option>
        </el-select>
      </template>


      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('assets:receive:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('assets:receive:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        width="50"
        label="序号" fixed="left">
      </el-table-column>
<!--      <el-table-column
        prop="receiveId"
        header-align="center"
        align="center"
        label="序号">
      </el-table-column>-->
      <el-table-column
        prop="assetsName"
        header-align="center"
        align="center"
        label="资产名">
      </el-table-column>
      <el-table-column
        prop="assetsNum"
        header-align="center"
        align="center"
        label="资产编号">
      </el-table-column>
      <el-table-column
        prop="userName"
        header-align="center"
        align="center"
        label="领用人">
      </el-table-column>
      <el-table-column
        prop="receiveTime"
        header-align="center"
        align="center"
        label="领用日期">
      </el-table-column>
<!--      <el-table-column
        prop="receiveDesc"
        header-align="center"
        align="center"
        label="领用说明">
      </el-table-column>-->
      <el-table-column
        prop="createStatus"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-tag  v-if="scope.row.createStatus == 0">审核中/领用</el-tag>
          <el-tag  v-if="scope.row.createStatus == 3">审核中/报修</el-tag>
          <el-tag type="success" v-if="scope.row.createStatus == 1">使用中</el-tag>
          <el-tag type="warning" v-if="scope.row.createStatus == 2">维修中</el-tag>
          <el-tag type="danger" v-if="scope.row.createStatus == 7">已报废</el-tag>
          <el-tag type="info" v-if="scope.row.createStatus == 4">不同意/领用</el-tag>  <!--领用失败-->
          <el-tag type="danger" v-if="scope.row.createStatus == 5">不同意/维修</el-tag>  <!--维修失败-->
          <el-tag type="info" v-if="scope.row.createStatus == 6">已归还</el-tag>

        </template>
      </el-table-column>
      <el-table-column
        prop="createBy"
        header-align="center"
        align="center"
        label="领用审核人">
      </el-table-column>
<!--      <el-table-column
        prop="autitorDesc"
        header-align="center"
        align="center"
        label="领用审核意见">
      </el-table-column>-->
<!--      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="领用审核时间">
      </el-table-column>-->
<!--      <el-table-column
        prop="repairsDesc"
        header-align="center"
        align="center"
        label="报修说明">
      </el-table-column>-->
      <el-table-column
        prop="repairsTime"
        header-align="center"
        align="center"
        label="报修日期">
      </el-table-column>
      <el-table-column
        prop="createBy2"
        header-align="center"
        align="center"
        label="报修审核人">
      </el-table-column>
<!--      <el-table-column
        prop="autitorDesc2"
        header-align="center"
        align="center"
        label="报修审核意见">
      </el-table-column>-->
<!--      <el-table-column
        prop="createTime2"
        header-align="center"
        align="center"
        label="报修审核日期">
      </el-table-column>-->
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button  type="text" size="medium" @click="addOrUpdateHandle(scope.row)">详情</el-button>

          <el-dropdown>
  <span class="el-dropdown-link" style="color:  #409EFF">
    更多<i class="el-icon-arrow-down el-icon--right"></i>
  </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item> <el-button v-if="isAuth('assets:repairs:repairsOk')" type="text" size="medium" @click="repairsOkHandle(scope.row.receiveId)" :disabled="scope.row.createStatus!=2">维修完成</el-button></el-dropdown-item>
              <el-dropdown-item><el-button v-if="isAuth('assets:repairs:scrapAdd')" type="text" size="medium" @click="scrapAddHandle(scope.row.receiveId)" :disabled="scope.row.createStatus!=2">报废</el-button></el-dropdown-item>
              <el-dropdown-item><el-button v-if="isAuth('assets:receive:autitor')" type="text" size="medium" @click="autitor(scope.row.receiveId)" :disabled="!(scope.row.createStatus == 3 || scope.row.createStatus == 0)">审核</el-button></el-dropdown-item>
              <el-dropdown-item ><el-button v-if="isAuth('assets:receive:repair')" type="text" size="medium" @click="repairHandle(scope.row.receiveId)" :disabled="scope.row.createStatus!=1">报修</el-button></el-dropdown-item>
              <el-dropdown-item ><el-button v-if="isAuth('assets:receive:return')" type="text" size="medium" @click="returnHandle(scope.row.receiveId)" :disabled="scope.row.createStatus!=1">归还</el-button></el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>

        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <receive-autitor v-if="autitorVisible" ref="receiveAutitor" @refreshDataList="getDataList"></receive-autitor>
    <repair-apply v-if="repairVisible" ref="repairApply" @refreshDataList="getDataList"></repair-apply>
    <receive-autitor2 v-if="autitorVisible2" ref="receiveAutitor2" @refreshDataList="getDataList"></receive-autitor2>

  </div>
</template>

<script>
  import AddOrUpdate from './receive-add-or-update'
  import receiveAutitor from './receiveAutitor'
  import repairApply from './repairApply'
  import receiveAutitor2 from './receiveAutitor2'

  export default {
    data () {
      return {
        options: [{
          key2: '',
          label: '全部'
        }, {
          key2: '0',
          label: '领用审核'
        }, {
          key2: '3',
          label: '报修审核'
        }, {
          key2: '1',
          label: '使用中'
        }, {
          key2: '2',
          label: '维修中'
        }, {
          key2: '7',
          label: '报废'
        }, {
          key2: '4',
          label: '不同意/领用'
        }, /* {
          key2: '5',
          label: '不同意/维修'
        }, */ {
          key2: '6',
          label: '已归还'
        }],
        dataForm: {
          key: '',
          key2: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addOrUpdateVisible: false,
        autitorVisible: false,
        repairVisible: false,
        autitorVisible2: false
      }
    },
    components: {
      AddOrUpdate,
      receiveAutitor,
      repairApply,
      receiveAutitor2
    },
    activated () {
      this.getDataList()
    },
    computed: {
      id: {   // 当前登录人的id
        get () { return this.$store.state.user.id }
      }
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl(`/assets/receive/list`),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'key': this.dataForm.key,  // 查询 输入的值
            'key2': this.dataForm.key2,  // 查询 输入的值
            'userId': this.$store.state.user.id, // 传回id判断角色
            'userName': this.$store.state.user.name  // 传回名字
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.dataList = data.page.list
            this.totalPage = data.page.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 审核
      autitor (id) {
        const selectedRow = this.dataList.find(row => row.receiveId === id)   // 点击审核拿到这一行的ID
       // console.log('一行' + selectedRow.createStatus)
        // eslint-disable-next-line no-mixed-operators
        if ((selectedRow.createStatus === '0') || (selectedRow.createStatus === '3')) {   // 不是领用/报修审核就不能点击
       /*    this.autitorVisible = true  */
          this.$nextTick(() => {
            if (selectedRow.createStatus === '0') {
              this.autitorVisible2 = true
              this.$refs.receiveAutitor2.init(id)
            } else {
              this.autitorVisible = true
              this.$refs.receiveAutitor.init(id)
            }
          })
        } else {
          this.$message({           // 如果审核过了 this.autitorVisible = false，其他才进入审核页面this.$refs.receiveAutitor.init(id)
            message: '你已经审核过了',
            onClose: () => {
              this.autitorVisible = false
            },
            type: 'warning'
          })
        }
      },
      // 维修完成
      repairsOkHandle (id) {
        const selectedRow = this.dataList.find(row => row.receiveId === id)   // 点击审核拿到这一行的ID
        this.$confirm('确定已经维修完成嘛？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (selectedRow && selectedRow.createStatus === '2') {   // 是维修中才能点击
            this.$http({
              url: this.$http.adornUrl('/assets/repairs/repairsOk'),
              method: 'post',
              data: this.$http.adornData({
                'repairsId': selectedRow.repairsId,
                'repairsTime': selectedRow.repairsTime,
                'userName': selectedRow.userName,
                'userMobile': selectedRow.userMobile,
                'repairsStatus': selectedRow.repairsStatus,
                'assetsNum': selectedRow.assetsNum,
                'assetsName': selectedRow.assetsName,
                'repairsDesc': selectedRow.repairsDesc,
                'repairsPic': selectedRow.repairsPic
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            this.$message({
              message: '该资产不用维修',
              type: 'warning'
            })
          }
        })
      },
      //  获得当前时间
      getCurrentTime () {
        return new Date()
      },
      // 报废
      scrapAddHandle (id) {
        const selectedRow = this.dataList.find(row => row.receiveId === id)   // 点击拿到维修表这一行的ID
        this.$confirm('确定已经报废完成嘛？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (selectedRow && selectedRow.createStatus === '2') {   // 是维修中才能点击
            this.$http({
              url: this.$http.adornUrl('/assets/repairs/scrapAdd'),
              method: 'post',
              data: this.$http.adornData({
                'scrapTime': this.getCurrentTime(),  // 报废时间
                'userName': selectedRow.userName,
                'assetsNum': selectedRow.assetsNum,
                'assetsName': selectedRow.assetsName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          } else {
            this.$message({
              message: '该资产不能报废',
              type: 'warning'
            })
          }
        })
      },
      // 报修
      repairHandle (id) {
        const selectedRow = this.dataList.find(row => row.receiveId === id)   // 点击领用拿到这一行的ID
        // console.log('mingzo' + selectedRow.assetsName)
        if (selectedRow && selectedRow.createStatus !== '1') {       // 状态不是使用中就不能点击
          this.$message({
            message: '该资产不能报修',
            onClose: () => {
              this.repairVisible = false
            },
            type: 'warning'
          })
        } else {
          this.repairVisible = true
          this.$nextTick(() => {
            this.$refs.repairApply.init(id)
          })
        }
      },
      // 归还
      returnHandle (id) {
        const selectedRow = this.dataList.find(row => row.receiveId === id)   // 点击审核拿到这一行的ID
        this.$confirm('确定对改资产归还？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if (selectedRow && selectedRow.createStatus !== '1') {   // 不是使用中就不能点击
            this.$message({
              message: '只有在使用，才可点击',
              type: 'warning'
            })
          } else {
            this.$http({
              url: this.$http.adornUrl('/assets/receive/assetsReturn'),
              method: 'post',
              data: this.$http.adornData({
                'receiveId': selectedRow.receiveId,
                'assetsName': selectedRow.assetsName,
                'userName': selectedRow.userName,
                'receiveTime': selectedRow.receiveTime,
                'createBy': selectedRow.createBy,
                'createTime': selectedRow.createTime,
                'assetsNum': selectedRow.assetsNum,
                'createStatus': '6',
                'receiveDesc': selectedRow.receiveDesc
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.getDataList()
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 新增 / 修改
      addOrUpdateHandle (row) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(row)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.receiveId
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/assets/receive/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '删除成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
<style>
.el-dropdown-link {
  cursor: pointer;
  //color: #409EFF;
}

</style>
